package main

import (
	"fmt"
)

const M = 10
const N = 10

var xpos, ypos int = 0, 0 //定义初始位置
var cangoout bool = false //假定走不出迷宫

//1代表人，2代表障碍，0空地

var data [M][N]int = [M][N]int{
	{1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 2, 0, 0, 2, 0, 0, 0},
	{0, 0, 0, 0, 2, 0, 0, 2, 2, 0},
	{0, 0, 0, 0, 2, 0, 2, 0, 0, 0},
	{0, 0, 0, 0, 2, 0, 0, 0, 0, 2},
	{2, 2, 2, 0, 0, 2, 0, 2, 0, 0},
	{0, 0, 0, 0, 0, 2, 0, 2, 0, 2},
	{0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 2, 2, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
}

var AIdata [M][N]int = [M][N]int{
	{1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 2, 0, 0, 2, 0, 0, 0},
	{0, 0, 0, 0, 2, 0, 0, 2, 2, 0},
	{0, 0, 0, 0, 2, 0, 2, 0, 0, 0},
	{0, 0, 0, 0, 2, 0, 0, 0, 0, 2},
	{2, 2, 2, 0, 0, 2, 0, 2, 0, 0},
	{0, 0, 0, 0, 0, 2, 0, 2, 0, 2},
	{0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 2, 2, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
}

func show(arr [M][N]int) {
	for i := 0; i < M; i++ {
		for j := 0; j < N; j++ {
			fmt.Printf("%4d", arr[i][j])
		}
		fmt.Println("")
	}

}

// wsad  //w 上，s下，a，左，d右边
func run(str string) {
	if str == "w" {
		if xpos-1 >= 0 && data[xpos-1][ypos] != 2 {
			data[xpos][ypos], data[xpos-1][ypos] = data[xpos-1][ypos], data[xpos][ypos]

			xpos = xpos - 1
		}
	} else if str == "s" {

		if xpos+1 < M && data[xpos+1][ypos] != 2 {
			data[xpos][ypos], data[xpos+1][ypos] = data[xpos+1][ypos], data[xpos][ypos]

			xpos = xpos + 1
		}

	} else if str == "a" {
		if ypos-1 >= 0 && data[xpos][ypos-1] != 2 {
			data[xpos][ypos], data[xpos][ypos-1] = data[xpos][ypos-1], data[xpos][ypos]

			ypos = ypos - 1
		}
	} else if str == "d" {
		if ypos+1 < N && data[xpos][ypos+1] != 2 {
			data[xpos][ypos], data[xpos][ypos+1] = data[xpos][ypos+1], data[xpos][ypos]

			ypos = ypos + 1
		}
	} else {
		fmt.Println("输入有误")
	}

	fmt.Println("--------------------------------", xpos, ypos)
	show(data)
}

//解决递归模拟走出来
func AIout(AIdatax [M][N]int, i int, j int) bool {
	AIdatax[i][j] = 3 //表示 走过
	if i == M-1 && j == N-1 {

		cangoout = true
		AIdata = AIdatax
		fmt.Println("迷宫可以走通")
	} else {

		if j+1 <= M-1 && AIdatax[i][j+1] < 2 && cangoout != true {

			AIout(AIdatax, i, j+1)

		}
		if i+1 <= N-1 && AIdatax[i+1][j] < 2 && cangoout != true {

			AIout(AIdatax, i+1, j)
		}
		if j-1 >= 0 && AIdatax[i][j-1] < 2 && cangoout != true {

			AIout(AIdatax, i, j-1)
		}
		if i-1 >= 0 && AIdatax[i-1][j] < 2 && cangoout != true {

			AIout(AIdatax, i-1, j)
		}
	}
	return cangoout
}

// ai自动根据递归生成自动路径
func AImoveOut() {
	AIdata[xpos][ypos] = 1

	for xpos != N-1 || ypos != M-1 {
		if ypos+1 <= M-1 && AIdata[xpos][ypos+1] == 3 {
			AIdata[xpos][ypos+1] = 0
			run("d")
		}

		if xpos+1 <= N-1 && AIdata[xpos+1][ypos] == 3 {
			AIdata[xpos+1][ypos] = 0
			run("s")
		}

		if ypos-1 >= 0 && AIdata[xpos][ypos-1] == 3 {
			AIdata[xpos][ypos-1] = 0
			run("a")
		}

		if xpos-1 >= 0 && AIdata[xpos-1][ypos] == 3 {
			AIdata[xpos-1][ypos] = 0
			run("w")
		}

		fmt.Println(xpos, "====", ypos)
	}

}

//手动寻路
func mainX() {
	show(data)

	for {
		var inputstr string

		if ypos == N-1 && xpos == M-1 {
			fmt.Println("恭喜您，走出迷宫")
			break
		}

		fmt.Scanln(&inputstr)

		run(inputstr)
	}

}

func mainXX() {
	isok := AIout(AIdata, 0, 0)
	fmt.Println("------------------------------------")
	if isok {
		show(AIdata)

		AImoveOut()

	} else {
		show(AIdata)
	}
	// time.Sleep(time.Second * 5)

}
